
package cn.droidlover.xrecyclerview;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Component;
import ohos.agp.components.AttrSet;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.VelocityDetector;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import java.math.BigDecimal;
import java.util.List;


public class XRecyclerView extends ListContainer implements Component.TouchEventListener {

    private VelocityDetector mVelocityTracker;
    private float mLastY = 0;
    float scrollY = 0;
    float scrollDistance = 50;
    private LoadingListener mLoadingListener;

    public XRecyclerView(Context context) {
        this(context, null);
    }


    public XRecyclerView(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }


    public XRecyclerView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.setTouchEventListener(this);

    }


    @Override
    public boolean onTouchEvent(Component component, TouchEvent ev) {
        final int action = ev.getAction();


        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityDetector.obtainInstance();
        }
        mVelocityTracker.addEvent(ev);
        switch (action) {

            case TouchEvent.PRIMARY_POINT_DOWN:
                mLastY = ev.getPointerPosition(ev.getIndex()).getY();

                break;
            case TouchEvent.POINT_MOVE:
                float current_Y = ev.getPointerPosition(ev.getIndex()).getY();
                scrollY = new BigDecimal(current_Y).subtract(new BigDecimal(mLastY) ).floatValue();

                break;

            case TouchEvent.PRIMARY_POINT_UP:
                if (scrollY > scrollDistance && !component.canScroll(Component.DRAG_DOWN)) {
                    mLoadingListener.onRefresh();
                }
                if (scrollY < -scrollDistance && !component.canScroll(Component.DRAG_UP)) {
                    mLoadingListener.onLoadMore();
                }

                mLastY = 0;
                break;

            case TouchEvent.CANCEL:

                break;
        }
        return true;
    }

    public void setLoadingListener(LoadingListener listener) {
        mLoadingListener = listener;
    }

    public interface LoadingListener {

        void onRefresh();

        void onLoadMore();
    }


    public static abstract class RecyclerAdapter<T> extends BaseItemProvider {
        private Context context;



        private List<T> data;


        public RecyclerAdapter(Context context, List<T> data) {
            this.context = context;
            this.data = data;

        }
        public Context getContext() {
            return context;
        }

        public void setContext(Context context) {
            this.context = context;
        }
        public List<T> getData() {
            return data;
        }

        @Override
        public T getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getCount() {
            return data == null ? 0 : data.size();
        }
    }

}